package com.wss.EE;

import java.util.*;
import java.util.stream.Collectors;


class Trader {
    private final String name;
    private final String city;

    public Trader(String name, String city) {
        this.name = name;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public String getCity() {
        return city;
    }

    @Override
    public String toString() {
        return "Trader{" +
                "name='" + name + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}


class Transaction {
    private final Trader trader;
    private final int year;
    private final int value;

    public Transaction(Trader trader, int year, int value) {
        this.trader = trader;
        this.year = year;
        this.value = value;
    }

    public Trader getTrader() {
        return trader;
    }

    public int getYear() {
        return year;
    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Transaction{" +
                "trader=" + trader +
                ", year=" + year +
                ", value=" + value +
                '}';
    }
}


public class Test {
    public static void main(String[] args) {
        // 初始化数据
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2021, 300),
                new Transaction(raoul, 2022, 1000),
                new Transaction(raoul, 2021, 400),
                new Transaction(mario, 2022, 710),
                new Transaction(mario, 2022, 700),
                new Transaction(alan, 2022, 950)
        );

        // (1) 找出2021年发生的所有交易，并按交易额排序（从低到高）
        List<Transaction> transactions2021 = transactions.stream()
                .filter(t -> t.getYear() == 2021)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());
        System.out.println("(1) 2021年的交易（按交易额升序）：" + transactions2021);

        // (2) 交易员都在哪些不同的城市工作过？
        Set<String> uniqueCities = transactions.stream()
                .map(t -> t.getTrader().getCity())
                .distinct()
                .collect(Collectors.toSet());
        System.out.println("(2) 不同的工作城市：" + uniqueCities);

        // (3) 查找所有来自于剑桥的交易员，并按姓名排序
        List<Trader> cambridgeTraders = transactions.stream()
                .map(Transaction::getTrader)
                .filter(t -> t.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());
        System.out.println("(3) 来自剑桥的交易员（按姓名排序）：" + cambridgeTraders);

        // (4) 返回所有交易员的姓名字符串，按字母顺序排序
        String traderNames = transactions.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .collect(Collectors.joining(", "));
        System.out.println("(4) 所有交易员的姓名（按字母排序）：" + traderNames);

        // (5) 有没有交易员是在米兰工作的？
        boolean hasMilanTrader = transactions.stream()
                .anyMatch(t -> t.getTrader().getCity().equals("Milan"));
        System.out.println("(5) 是否有米兰的交易员：" + hasMilanTrader);

        // (6) 打印生活在剑桥的交易员的所有交易额
        int cambridgeTotalValue = transactions.stream()
                .filter(t -> t.getTrader().getCity().equals("Cambridge"))
                .mapToInt(Transaction::getValue)
                .sum();
        System.out.println("(6) 剑桥交易员的总交易额：" + cambridgeTotalValue);

        // (7) 所有交易中，最高的交易额是多少？
        Optional<Integer> maxValue = transactions.stream()
                .map(Transaction::getValue)
                .max(Integer::compareTo);
        System.out.println("(7) 最高交易额：" + maxValue.orElse(0));

        // (8) 找到交易额最小的交易
        Optional<Transaction> minTransaction = transactions.stream()
                .min(Comparator.comparing(Transaction::getValue));
        System.out.println("(8) 最小交易额的交易：" + minTransaction.orElse(null));
    }
}
